import { AppState } from "@/interface";
import shared from "@/shared";
import router from '@/routes';
import { AppService } from "./app-service";
import { apps } from "@/micro";

/**
 * 微应用行为处理服务
 *
 * @export
 * @class MicroActionService
 */
export class MicroActionService {

    /**
     * 唯一实例
     * 
     * @private
     * @static
     * @memberof MicroActionService
     */
    private static readonly instance = new MicroActionService();

    /**
     * 获取唯一实例
     *
     * @static
     * @return {*}  {MicroActionService}
     * @memberof MicroActionService
     */
    public static getInstance(): MicroActionService {
        return MicroActionService.instance;
    }

    /**
     * 处理微应用抛出行为
     *
     * @param {AppState} state
     * @memberof MicroActionService
     */
    public handleAction(state: AppState) {
        const { action, data } = state;
        switch (action) {
            case 'ADD_PAGE':
                shared.addPage(data);
                break;
            case 'UPDATE_PAGE':
                shared.updatePage(data);
                break;
            case 'REMOVE_PAGE':
                this.handleRemovePage(data);
                break;
            case 'LOGOUT':
                this.handleLogout();
                break;
            default:
                console.log(`基座应用${action}暂未支持`);
                break;
        }
    }

    /**
     * 处理关闭页面(由微应用触发)
     *
     * @param {*} data
     * @memberof MicroActionService
     */
    public handleRemovePage(data:any){
        shared.removePage(data);
        const allPages = shared.getAllPage();
        const activeMicroApp = AppService.getInstance().getMountedMicroApp();
        // 无页面，跳转当前激活应用默认页面
        if(!allPages || allPages.length === 0){
            const activeRule = activeMicroApp?.props?.activeRule;
            if(activeRule){
                router.push(activeRule);
            }
        }else{
            const existAppIndex = allPages.findIndex((page:any) =>{
                return page.microAppName === activeMicroApp.name;
            })
            // 当前激活应用无激活页面，应跳转其他应用
            if(existAppIndex === -1){
                const lastPage = allPages[allPages.length - 1];
                if(lastPage.microAppName){
                    const activeApp = apps.find((app: any) => {
                        return app.name === lastPage.microAppName;
                    });
                    if (activeApp) {
                        router.push(activeApp.activeRule);
                      }
                      AppService.getInstance().noticeMicroApp({
                        tag: lastPage.microAppName,
                        action: "TABCLICK",
                        data: lastPage
                      });
                }
            }
        }
    }

    /**
     * 处理登出
     *
     * @memberof MicroActionService
     */
    public handleLogout(){
        AppService.getInstance().clearAppData();
        shared.removeAllPage();
        router.push({name:'Login'});
    }

}